/**
 * 运动记录服务
 */

import { BaseApiService, PageResponse } from '../utils/api';
import {
  UserExerciseVo,
  UserExerciseBo,
} from '../types/api';

export interface ExerciseListParams {
  pageNum?: number;
  pageSize?: number;
  startDate?: string; // yyyy-MM-dd
  endDate?: string; // yyyy-MM-dd
}

export class ExerciseService extends BaseApiService {
  
  /**
   * 获取运动记录列表（分页）
   */
  async getExerciseRecords(params: ExerciseListParams = {}): Promise<PageResponse<UserExerciseVo>> {
    const { pageNum = 1, pageSize = 10, startDate, endDate } = params;
    return this.handleResponse(
      this.http.get<PageResponse<UserExerciseVo>>('/api/exercise', {
        pageNum,
        pageSize,
        startDate,
        endDate,
      })
    );
  }

  /**
   * 获取运动记录详情
   */
  async getExerciseRecord(id: number): Promise<UserExerciseVo> {
    return this.handleResponse(
      this.http.get<UserExerciseVo>(`/api/exercise/${id}`)
    );
  }

  /**
   * 添加运动记录
   */
  async addExerciseRecord(data: UserExerciseBo): Promise<void> {
    return this.handleResponse(
      this.http.post<void>('/api/exercise', data)
    );
  }

  /**
   * 更新运动记录
   */
  async updateExerciseRecord(id: number, data: UserExerciseBo): Promise<void> {
    return this.handleResponse(
      this.http.put<void>(`/api/exercise/${id}`, data)
    );
  }

  /**
   * 删除运动记录
   */
  async deleteExerciseRecord(id: number): Promise<void> {
    return this.handleResponse(
      this.http.delete<void>(`/api/exercise/${id}`)
    );
  }

  /**
   * 统计运动时长
   */
  async getTotalExerciseDuration(startDate?: string, endDate?: string): Promise<number> {
    return this.handleResponse(
      this.http.get<number>('/api/exercise/statistics/duration', {
        startDate,
        endDate,
      })
    );
  }
}

// 导出单例实例
export const exerciseService = new ExerciseService();
